<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/7
 * Time: 19:22
 */

namespace app\api\controller;



use think\Db;

class Hot extends Base
{
    private $user_id;
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function _initialize()
    {
        $this->user_id = input('user_id');
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    /**
     * 动态列表
     * @return \think\response\Json
     * hot_type     0 全部  1 热门  2 关注
     * page         页码
     */
    public function hotList(){
        $type = input('type'); // 1 热门  2 全部
        $page = input('page'); // 1 热门  2 全部
        $user_id = input('user_id'); // 1 热门  2 全部
        if($type == 1){
            $order = 'h_likes desc';
        }else{
            $order = 'h_time desc';
        }
        $list = Db::table('up_hot')
            ->join('up_u_user', 'h_user_id = user_id', 'left')
            ->where(function ($query) use ($user_id) {
                $query->where('h_user_id', $user_id);
            })
            ->whereOr(function ($query) use ($user_id) {
                $query->where('h_user_id', '<>',$user_id)->where('h_status', 2);
            })
            ->field('user_username, user_image, h_time, h_title,h_type, h_url,h_id,user_id')
            ->page($page, 10)
            ->order($order)
            ->select();
        if($list){
            foreach($list as $k => $v){
                if($v['h_type'] == 1){
                    $img = trim($v['h_url'], ',');
                    if($img){
                        $list[$k]['h_url'] = explode(',', $img);
                    }else{
                        $list[$k]['h_url'] = [];
                    }

                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    /**
     * 动态详情
     * @return \think\response\Json
     * h_id   动态ID
     * page   页码
     */
    public function hotDetail(){
        $h_id = $_POST['h_id'];
        // 详情
        $detail = Db::table('up_hot')->alias('h')
            ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
            ->field('user_username, user_image, h.*')
            ->where('h_id', $h_id)
            ->find();
        if($detail){
            if($detail['h_type'] == 1){
                $img = trim($detail['h_url'], ',');
                if($img){
                    $detail['h_url'] = explode(',', $img);
                }else{
                    $detail['h_url'] = [];
                }
            }
            // 是否关注
            $isLike = Db::table('up_like')
                ->where('l_user_id', $this->user_id)
                ->where('l_h_id', $h_id)
                ->find();
            $like = 1;
            if($isLike){
                $like = 2; // 已点赞
            }
            $detail['like'] = $like;
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 100, 'msg' => '此动态已不存在']);
        }
    }
    /**
     * 关注/取消关注
     * @return \think\response\Json
     * other_user_id    动态发布者ID
     */
    public function hotLike(){
        $h_id = $_POST['h_id'];
        $have = Db::table('up_like')
            ->where('l_user_id', $this->user_id)
            ->where('l_h_id', $h_id)
            ->field('l_id')
            ->find();
        if($have){
            $res = Db::table('up_like')->where('l_id', $have['l_id'])->delete();
            Db::table('up_hot')
                ->where('h_id', $h_id)
                ->setDec('h_likes',1);
        }else{
            $insert = [
                'l_user_id' => $this->user_id,
                'l_h_id' => $h_id,
                'l_time' => $this->create_time
            ];
            $res = Db::table('up_like')->insert($insert);
            Db::table('up_hot')
                ->where('h_id', $h_id)
                ->setInc('h_likes',1);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }

    /**
     * 发布
     * @return \think\response\Json
     * h_title        标题
     * h_url        图片
     * h_type         类型   1 图片  2 视频
     */
    public function userPublish(){
        $h_title = trim(input('h_title'));
        $h_url = trim(input('h_url'),' ');
        $h_type = input('h_type'); // 1 图片  2 视频
        $data = [
            'h_time' => date('Y-m-d H:i:s'),
            'h_url' => $h_url,
            'h_title' => $h_title,
            'h_user_id' => $this->user_id,
            'h_type' => $h_type
        ];
        $res = Db::table('up_hot')->insert($data);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }

    public function indata()
    {
        $type = 1;
        $data = [
            'plan_p_id' => 1,
            'plan_number' => time(),
            'plan_typename' => '课程类型',
            'plan_teacher' => 2,
            'plan_student' => 1,
            'plan_address' => '哈尔滨大街',
            'plan_lat' => '37.185929',
            'plan_lon' => '122.255333',
            'plan_start_time' => '2020-07-07 17:00:00',
            'plan_minus' => 45,
            'plan_end_time' => '2020-07-07 17:45:00',
            'plan_real_start' => '',
            'plan_real_end' => '',
            'plan_list' => 1,
            'plan_type' => $type,
            'plan_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_plan_info')->insert($data);
        dump($res);
    }

}